import { defineStore } from "pinia";
import { ref } from "vue";
import { getLoginUser } from "@/api/userController";

/**
 * 存储用户信息的状态
 */
export const useLoginUserStore = defineStore(
  "loginUser",
  () => {
    const loginUser = ref<API.UserLoginRespDTO>({
      userName: "未登录",
      id: 0,
    });

    //set方法
    function setLoginUser(newLoginUser: any) {
      loginUser.value = newLoginUser;
    }

    //获取登录用户
    async function fetchLoginUser() {
      const res = await getLoginUser();
      console.log(res)
      if (res.data.code === "0" && res.data.data) {
        loginUser.value = res.data.data;
      }
    }

    // 返回需要暴露的对象
    return {
      loginUser,
      setLoginUser,
      fetchLoginUser
    };
  },

  //参数持久化
  {
    persist: true
  }
);
